package com.bestdreamer.str;

/**
 * Java 字符串倒置
 */
public class StrReverse {

    public static void main(String[] args){
        String str = "hello world";

        System.out.println(reverseStr(str));

        System.out.println(reverseWord(str));

    }

    /**
     * 字符串倒置
     */
    public static String reverseStr(String str){
        StringBuilder sb = new StringBuilder(str);
        return sb.reverse().toString();
    }

    /**
     * 单词倒置
     * \b 单词边界
     * \B 非单词边界
     */
    public static String reverseWord(String str){
        String[] arr = str.split("\\b"); //按单词边界分割
        String newStr = "";
        for(int i=arr.length-1;i>=0;i--){
            newStr += arr[i];
        }
        return newStr;
    }
}
